import re

class parser:
    def __init__(self):
        pattern = r"""  # use raw string
            (\d{3})     # area code. Use \d rather than [0-9], because \d still works for UNICODE.
            \D*         # separator is optional. For the same reason, use \D rather than [^0-9]
            (\d{3})     # trunk code
            \D*
            (\d{4})        # phone number
            \D*
            (\d*)        # ext number is optional with any number of digits
            $           # may be 
        """
        self.pat = re.compile(pattern, re.VERBOSE)

    def parse(self, n):
        #print "n is \"%s\"" %n
        return self.pat.search(n).groups()

if __name__ == "__main__":
    numbers = [
    "800-555-1212",
    "800 555 1212",
    "800.555.1212",
    "(800) 555-1212",
    "1-800-555-1212",
    "800-555-1212-1234",
    "800-555-1212x1234",
    "800-555-1212 ext. 1234",
    "work 1-(800) 555.1212 #1234",
    "80055512121234",
    "(086)010-62671140",
    ]
    
    p = parser()
    for n in numbers:
        print "\t".join((n.ljust(40),) +  p.parse(n))

    
